package net.lab1024.sa.admin.module.fabricmaterials.service;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import net.lab1024.sa.admin.module.business.goods.constant.GoodsStatusEnum;
import net.lab1024.sa.admin.module.business.goods.domain.entity.GoodsEntity;
import net.lab1024.sa.admin.module.business.goods.domain.vo.GoodsExcelVO;
import net.lab1024.sa.admin.module.fabricmaterials.dao.FabricMaterialsDao;
import net.lab1024.sa.admin.module.fabricmaterials.domain.entity.FabricMaterialsEntity;
import net.lab1024.sa.admin.module.fabricmaterials.domain.form.FabricMaterialsAddForm;
import net.lab1024.sa.admin.module.fabricmaterials.domain.form.FabricMaterialsQueryForm;
import net.lab1024.sa.admin.module.fabricmaterials.domain.form.FabricMaterialsUpdateForm;
import net.lab1024.sa.admin.module.fabricmaterials.domain.vo.FabricMaterialsVO;
import net.lab1024.sa.base.common.code.ErrorCode;
import net.lab1024.sa.base.common.util.SmartBeanUtil;
import net.lab1024.sa.base.common.util.SmartEnumUtil;
import net.lab1024.sa.base.common.util.SmartPageUtil;
import net.lab1024.sa.base.common.domain.ResponseDTO;
import net.lab1024.sa.base.common.domain.PageResult;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

/**
 * lyy Service
 *
 * @Author lyy
 * @Date 2024-11-12 22:01:35
 * @Copyright lyy
 */

@Service
public class FabricMaterialsService {

    @Resource
    private FabricMaterialsDao fabricMaterialsDao;

    /**
     * 分页查询
     *
     * @param queryForm
     * @return
     */
    public PageResult<FabricMaterialsVO> queryPage(FabricMaterialsQueryForm queryForm) {
        Page<?> page = SmartPageUtil.convert2PageQuery(queryForm);
        List<FabricMaterialsVO> list = fabricMaterialsDao.queryPage(page, queryForm);
        PageResult<FabricMaterialsVO> pageResult = SmartPageUtil.convert2PageResult(page, list);
        return pageResult;
    }

    /**
     * 获取库存
     */

    public ResponseDTO<Integer> getFabricMaterialsQuantity(Integer id){

        Integer quantity = fabricMaterialsDao.getFabricMaterialsQuantity(id);
        return ResponseDTO.ok(quantity);
    }

    /**
     * 获取物料数据
     */
    public ResponseDTO<String> getFabricMaterialCode(String fabricMaterialCode){
        String code = fabricMaterialsDao.queryFabricMaterialCode(fabricMaterialCode);
        return ResponseDTO.ok(code);
    }

    public ResponseDTO<FabricMaterialsVO> getFabricMaterials(String fabricMaterialCode){
        String code = fabricMaterialsDao.queryFabricMaterialCode(fabricMaterialCode);
        if(code.equals("1") && code!= null ) {
            FabricMaterialsVO fabricMaterials = fabricMaterialsDao.getFabricMaterials(fabricMaterialCode);

            return  ResponseDTO.ok(fabricMaterials);
        }else{
            return ResponseDTO.userErrorParam("该物料代码存在多种物料");

        }
    }
    /**
     * 获取物料数据
     */
    public ResponseDTO<List<FabricMaterialsVO>> queryAll(){
        List<FabricMaterialsVO> fabricMaterials = fabricMaterialsDao.queryAll();
        return  ResponseDTO.ok(fabricMaterials);
    }

    /**
     * 获取所有的物料代码
     *
     */
    public ResponseDTO<List<FabricMaterialsVO>> getAllFabricMaterials(){
        List<FabricMaterialsVO> fabricMaterials = fabricMaterialsDao.getAllFabricMaterials();
        return ResponseDTO.ok(fabricMaterials);
    }


    public List<FabricMaterialsVO> selectList() {
        List<FabricMaterialsVO> EntityList = fabricMaterialsDao.selectList();
        return EntityList.stream().collect(Collectors.toList());

    }



    /**
     * 添加
     */
    public ResponseDTO<String> add(FabricMaterialsAddForm addForm) {
        FabricMaterialsEntity fabricMaterialsEntity = SmartBeanUtil.copy(addForm, FabricMaterialsEntity.class);
        fabricMaterialsDao.insert(fabricMaterialsEntity);
        return ResponseDTO.ok();
    }

    /**
     * 更新
     *
     * @param updateForm
     * @return
     */
    public ResponseDTO<String> update(FabricMaterialsUpdateForm updateForm) {
        FabricMaterialsEntity originalFabricMaterialsEntity = fabricMaterialsDao.selectById(updateForm.getId());
        FabricMaterialsEntity fabricMaterialsEntity = SmartBeanUtil.copy(updateForm, FabricMaterialsEntity.class);
        fabricMaterialsEntity.setQuantity(fabricMaterialsEntity.getQuantity() + originalFabricMaterialsEntity.getQuantity());
        fabricMaterialsDao.updateById(fabricMaterialsEntity);
        return ResponseDTO.ok();
    }

    /**
     * 批量删除
     *
     * @param idList
     * @return
     */
    public ResponseDTO<String> batchDelete(List<String> idList) {
        if (CollectionUtils.isEmpty(idList)){
            return ResponseDTO.ok();
        }

        fabricMaterialsDao.deleteBatchIds(idList);
        return ResponseDTO.ok();
    }

    /**
     * 单个删除
     */
    public ResponseDTO<String> delete(String id) {
        if (null == id){
            return ResponseDTO.ok();
        }

        fabricMaterialsDao.deleteById(id);
        return ResponseDTO.ok();
    }

    public ResponseDTO<FabricMaterialsEntity> getFabricById(Integer id){
        FabricMaterialsEntity fabricMaterialsEntity = fabricMaterialsDao.selectById(id);
        return ResponseDTO.ok(fabricMaterialsEntity);
    }
}
